package 补充.多线程;
class Ticket1 implements Runnable{
    private int ticket=5;
    @Override
    public void run() {
        while(true){
            synchronized (this){
                if(ticket>0){
                    try {
                        Thread.sleep(100);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票，还剩"+--ticket+"张票");
                }
            }
        }
    }
}
public class 同步代码块 {

    public static void main(String[] args) {
        Ticket1 ticket = new Ticket1();
        Thread t1 = new Thread(ticket,"窗口一");
        Thread t2 = new Thread(ticket,"窗口二");
        Thread t3 = new Thread(ticket,"窗口三");
        t1.start();
        t2.start();
        t3.start();
    }
}
